import cv2
import numpy as np
from utils.common import load_image, show_image, make_dirs
from utils.config import exp5_output_dir, exp5_hof_path

# 确保输出目录存在
make_dirs(exp5_output_dir)

# 加载图片
image = load_image()

# 将图像转换为灰度图
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 边缘检测
edges = cv2.Canny(gray_image, 50, 150)

# 霍夫变换检测直线
lines = cv2.HoughLines(edges, 1, np.pi/180, 100)

if lines is not None:
    for line in lines:
        rho, theta = line[0]
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a * rho
        y0 = b * rho
        x1 = int(x0 + 1000 * (-b))
        y1 = int(y0 + 1000 * (a))
        x2 = int(x0 - 1000 * (-b))
        y2 = int(y0 - 1000 * (a))
        cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)

show_image("Hough Transform Feature Image", image)

# 保存结果
cv2.imwrite(exp5_hof_path, image)
print(f"基于霍夫变换的特征提取结果已保存到：{exp5_hof_path}")

